import 'dart:convert' as convert;
import 'dart:convert';
import 'package:crypto/crypto.dart';
String toPriceParse<T>(T){

  if (T is double){
    if (T > 10000.0){
      double decimal = T / 10000.0;
      return decimal.toStringAsFixed(3) + "万";
    }else{
      return "${T.toStringAsFixed(4)}";
    }
  }else if(T is int){
    if (T>10000) {
      double number = T.toDouble() / 10000.0;
      return "${number.toStringAsFixed(3)}" + "w";
    }
      return "$T";

  }
  else if (T is String){
    double price = double.parse(T);
    if (price >= 10000.0){
      double decimal = price / 10000.0;
      return decimal.toStringAsFixed(3) + "万";
    }
    return T;
  }
  return "$T";
}

extension StringUtils on String{
  String toPrice(){
    return toPriceParse(this);
  }

  String toMd5(){
    return md5.convert(utf8.encode(this)).toString();
  }

  /*
  * Base64加密
  */
  static String encodeBase64(String data){
    var content = utf8.encode(data);
    var digest = base64Encode(content);
    return digest;
  }

  /*
  * Base64解密
  */
  static String decodeBase64(String data){
    return String.fromCharCodes(base64Decode(data));
  }

  static Map<String,dynamic> toMap(String data){
    return convert.jsonDecode(data);
  }
  static String toJsonStr(Map<String,dynamic> data){
    return convert.jsonEncode(data);
  }
  static String toPercentSign(dynamic data){
    int percent = 0;
    if(data is double){
      percent = (data * 100) as int;
    }else if(data is String){
      try {
        percent = (double.parse(data) * 100.0).toInt();
      }catch (e){
        percent = 0;
      }
    }
    return "$percent%";
  }
}

extension DoubleUtils on double{
  String toPrice(){
    return toPriceParse(this);
  }
}
extension IntUtils on int{
  String toPrice(){
    return toPriceParse(this);
  }
}